from __future__ import print_function
import os
import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.common")
django.setup(set_prefix=False)

from apps.news.news_pb2 import *
from apps.news.news_pb2_grpc import *
from django.conf import settings

_PORT = '50051'

def run():
    conn = grpc.insecure_channel(settings._HOST + ':' + settings._PORT)
    client = NewsStub(channel=conn)
    response = client.NewsSearch(NewsSearchRequest(query='David', page=1, page_size=10))
    results = response.results
    for item in results:
        print(item.id, item.title)


if __name__ == '__main__':

    if len(sys.argv)== 2:
        print (sys.argv[1])
        _HOST = sys.argv[1]
    else:
        _HOST = 'localhost'

    run()